<?php
class BaseModelHttp {
    /**
     * Http get method request 
     * 
     * @param string $url
     * @param int $timeout
     * @param reference string $reason
     * @param reference int $status
     * @return response result of false on requst failure
     */
	public static function get($url, $timeout, &$reason, &$status) {
		$info = parse_url($url);
		if (!$info["port"]) {
			$info["port"] = "80";
		}
		$fp = fsockopen ($info["host"], $info["port"], $errno, $errstr, 3);
		if (!$fp) {
			$reason = "connect";
			return false;
		}
		if (strlen($info["query"])) {
			$head = "GET ".$info[path]."?".$info["query"]." HTTP/1.0\r\n";
		}
		else {
			$head = "GET ".$info[path]." HTTP/1.0\r\n";
		}
		$head .= "Host: ".$info[host]."\r\n";
		$head .= "\r\n";
		$write = fputs($fp, $head);
		if ($write != strlen($head)) {
			$reason = "write";
			return false;
		}
		stream_set_timeout($fp, $timeout);
		$result = "";
		$i = 0;
		while (!feof($fp)) {
			$line = fread($fp,4096);
			if ($i==0) {
				list($protocol, $status, $statuswork) = explode(" ", $line, 3);
				if ($status >= 300) {
					fclose($fp);
					$reason = "status";
					return false;
				}
				$i = 1;
			}
			$result .= $line;
			$metadata = stream_get_meta_data($fp);
			if ($metadata["timed_out"]) {
				fclose($fp);
				$reason = "timeout";
				return false;
			}
		}
		fclose($fp);
		$result = explode("\r\n\r\n", $result, 2);
		$result = $result[1];
		return $result;
	}
    /**
     * Http post method requst
     * 
     * @param string $url
     * @param string $query
     * @param int $timeout
     * @param reference string $reason
     * @param reference int $status
     * @return response result or false on failure
     */
	public static function post($url, $query, $timeout, &$reason, &$status) {
		$info = parse_url($url);
		if (!$info["port"]) {
			$info["port"] = "80";
		}
		$fp = fsockopen($info["host"], $info["port"], $errno, $errstr, 3);
		if (!$fp) {
			$reason = "connect";
			return false;
		}
		if (strlen($info["query"])) {
			$head = "POST ".$info[path]."?".$info["query"]." HTTP/1.0\r\n";
		}
		else {
			$head = "POST ".$info[path]." HTTP/1.0\r\n";
		}
		$head .= "Host: ".$info[host]."\r\n";
		$head .= "Content-type: application/x-www-form-urlencoded\r\n";
		$head .= "Content-Length: ".strlen(trim($query))."\r\n";
		$head .= "\r\n";
		$head .= trim($query);
		$write = fputs($fp, $head);
		if ($write != strlen($head)) {
			$reason = "write";
			return false;
		}
		stream_set_timeout($fp, $timeout);
		$result = "";
		$i = 0;
		while (!feof($fp)) {
			$line = fread($fp,4096);
			if ($i==0) {
				list($protocol, $status, $statuswork) = explode(" ", $line, 3);
				if ($status >= 300) {
					fclose($fp);
					$reason = "status";
					return false;
				}
				$i = 1;
			}
			$result .= $line;
			$metadata = stream_get_meta_data($fp);
			if ($metadata["timed_out"]) {
				fclose($fp);
				$reason = "timeout";
				return false;
			}
		}
		fclose($fp);
		$result = explode("\r\n\r\n", $result, 2);
		$result = $result[1];
		return $result;
	}
}
?>